<?php

namespace App\Models\Base;
use App\Models\BaseModel;
use App\Models\Role\RoleResourceBind;
use App\Params\Backend\MenuListParams;
use App\Params\Backend\MenuUpdateParam;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * 菜单模型
 *
 * @package App\Models
 */
class Menu extends BaseModel
{
    protected $table = 'base_menu';
    protected $primaryKey = 'menu_id';

    /**
     * 根据层级获取菜单
     *
     * @param int $level
     * @param string $isActive
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getByLevel(int $level, string $isActive = 'all')
    {
        $menu = Menu::query()->orderBy('sort_num', 'asc');
        if($level) {
            $menu->where('level', $level);
        }
        if($isActive != 'all') {
            $menu->where('is_active', $isActive);
        }
        return $menu->get();
    }

    /**
     * 根据过滤条件获取菜单列表
     *
     * @param MenuListParams $param
     * @return
     */
    public static function getByFilter(MenuListParams $param)
    {
        $menu = Menu::query()->orderBy('sort_num', 'asc');
        if($param->getName()) {
            $menu->where('menu_name', 'like', '%' . $param->getName() . '%');
        }
        $menu->select($param->getFields());
        return $menu;
    }

    /**
     * 获取子菜单
     *
     * @param int $parentId
     * @param string $isActive
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getByParentId(int $parentId, string $isActive = 'all')
    {
        $menu = Menu::query();
        if($isActive != 'all') {
            $menu->where('is_active', $isActive);
        }
        return $menu->where('parent_id', $parentId)->orderBy('sort_num', 'asc')->get();
    }

    /**
     * 根据ID获取菜单
     *
     * @param int $menuId
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public static function getById(int $menuId)
    {
        return Menu::query()->where('menu_id', $menuId)->first();
    }

    /**
     * 启用/停用菜单
     *
     * @param int $menuId
     * @param int $isActive
     * @return int
     */
    public static function enable(int $menuId, int $isActive = 1)
    {
        return Menu::query()->where('menu_id', $menuId)->update(['is_active' => $isActive]);
    }

    /**
     * 删除菜单
     * 1、删除菜单、删除菜单下的资源、删除角色已绑定的资源
     *
     * @param int $menuId
     * @return mixed
     */
    public static function remove(int $menuId)
    {
        $result = 0;
        try {
            DB::beginTransaction();
            // 删除菜单
            Menu::query()->where('menu_id', $menuId)->where('is_system', 0)->delete();
            // 获取菜单下的资源ID
            $resourceIds = Resource::getByMenuId($menuId)->get()->map(function ($item){
                return $item->resource_id;
            })->toArray();
            // 删除菜单下的资源
            Resource::removeByMenuId($menuId);
            // 删除角色已绑定的资源
            RoleResourceBind::removeByIds($resourceIds);
            DB::commit();
            $result = 1;
        } catch (\Exception $exception) {
            DB::rollBack();
        }
        return $result;
    }

    /**
     * 保存菜单
     *
     * @param MenuUpdateParam $param
     * @return int|mixed
     */
    public static function saveData(MenuUpdateParam $param)
    {
        $query = $param->getId() ? Menu::find($param->getId()) : new Menu();
        foreach ($param as $key => $value) {
            $query->{$key} = $value;
        }
        return $query->save() ? $query->menu_id : 0;
    }
}
